<html>
<head><meta charset="utf-8"><title>Get a crate&#x27;s public type information? · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Get.20a.20crate&#x27;s.20public.20type.20information.3F.html">Get a crate&#x27;s public type information?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="218338068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Get%20a%20crate%27s%20public%20type%20information%3F/near/218338068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Sherret <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Get.20a.20crate&#x27;s.20public.20type.20information.3F.html#218338068">(Nov 30 2020 at 20:03)</a>:</h4>
<p>Hello, apologies if this is not the right place to ask, but I know people in here would know the answer. I am looking to programatically generate a crate based on another crate. Is there an API available in rust-analyzer or something rust-analyzer uses to get type information? This must be possible since the crate documentation on <a href="http://crates.io">crates.io</a> seems to be able to resolve all the information I would need... I just have no idea how easily possible. Thanks!</p>



<a name="218427714"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Get%20a%20crate%27s%20public%20type%20information%3F/near/218427714" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Get.20a.20crate&#x27;s.20public.20type.20information.3F.html#218427714">(Dec 01 2020 at 14:22)</a>:</h4>
<p>You can use rust-analyzer for this, with a caveat that:</p>
<ul>
<li>there are zero backwards compatibility guarantees</li>
<li>there are next to zero docs</li>
</ul>



<a name="218427824"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Get%20a%20crate%27s%20public%20type%20information%3F/near/218427824" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Get.20a.20crate&#x27;s.20public.20type.20information.3F.html#218427824">(Dec 01 2020 at 14:23)</a>:</h4>
<p>A good place to start would be <code>rust-analyzer analysis-stats .</code> command</p>
<p><a href="https://github.com/rust-analyzer/rust-analyzer/blob/bb697727a2e43d734a1bf3aae7644a9249aee11e/crates/rust-analyzer/src/cli/analysis_stats.rs#L149">https://github.com/rust-analyzer/rust-analyzer/blob/bb697727a2e43d734a1bf3aae7644a9249aee11e/crates/rust-analyzer/src/cli/analysis_stats.rs#L149</a></p>
<p>It traverses the crate and infers types of all of the functions defined therein.</p>



<a name="218429294"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Get%20a%20crate%27s%20public%20type%20information%3F/near/218429294" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Get.20a.20crate&#x27;s.20public.20type.20information.3F.html#218429294">(Dec 01 2020 at 14:34)</a>:</h4>
<p>The rustdoc JSON backend will hopefully land this week and have the information you want.</p>



<a name="218429361"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Get%20a%20crate%27s%20public%20type%20information%3F/near/218429361" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Get.20a.20crate&#x27;s.20public.20type.20information.3F.html#218429361">(Dec 01 2020 at 14:35)</a>:</h4>
<p>The format is likely to change significantly before being stabilized.<br>
<a href="https://github.com/rust-lang/rust/pull/79539#pullrequestreview-541878688">https://github.com/rust-lang/rust/pull/79539#pullrequestreview-541878688</a></p>



<a name="218429855"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Get%20a%20crate%27s%20public%20type%20information%3F/near/218429855" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Get.20a.20crate&#x27;s.20public.20type.20information.3F.html#218429855">(Dec 01 2020 at 14:39)</a>:</h4>
<p>I'm still slightly nervous about how this interacts with --document-private-items, but since it's unstable I'm ok with it for now.</p>



<a name="218498296"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Get%20a%20crate%27s%20public%20type%20information%3F/near/218498296" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Sherret <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Get.20a.20crate&#x27;s.20public.20type.20information.3F.html#218498296">(Dec 01 2020 at 23:46)</a>:</h4>
<p><span class="user-mention" data-user-id="133169">@matklad</span> thanks! I'm interested in inspecting the structs. I don't mind too much depending on unstable api. On a sidenote, it would be great if Rust had something like TypeScript does with ts-morph (<a href="https://github.com/dsherret/ts-morph/tree/latest/packages/ts-morph#example">https://github.com/dsherret/ts-morph/tree/latest/packages/ts-morph#example</a>) and some wrapper around rust-analyzer in the future might make that possible. Huge amount of work to implement though!</p>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span> oh, that is awesome! It's a little strange for me to rely on something like that, but it also means I could get the output and quickly write up something in a scripting language to do the code generation. My use case is also quite simple. Thanks for bringing that to my attention! I think I'm going to go with that.</p>



<a name="218734761"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Get%20a%20crate%27s%20public%20type%20information%3F/near/218734761" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Get.20a.20crate&#x27;s.20public.20type.20information.3F.html#218734761">(Dec 03 2020 at 18:27)</a>:</h4>
<p>I have visitor over the rust ast from rust-analyzer at <a href="https://github.com/pksunkara/cargo-up/tree/master/cargo-up/rust-visitor">https://github.com/pksunkara/cargo-up/tree/master/cargo-up/rust-visitor</a>. And then I also have a little bit of code like tsmorph at <a href="https://github.com/pksunkara/cargo-up/blob/master/cargo-up/src/lib/runner.rs#L277">https://github.com/pksunkara/cargo-up/blob/master/cargo-up/src/lib/runner.rs#L277</a>. The linked function renames a struct</p>



<a name="218734788"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Get%20a%20crate%27s%20public%20type%20information%3F/near/218734788" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Get.20a.20crate&#x27;s.20public.20type.20information.3F.html#218734788">(Dec 03 2020 at 18:27)</a>:</h4>
<p><span class="user-mention" data-user-id="368518">@David Sherret</span> ^^</p>



<a name="218737743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Get%20a%20crate%27s%20public%20type%20information%3F/near/218737743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Get.20a.20crate&#x27;s.20public.20type.20information.3F.html#218737743">(Dec 03 2020 at 18:51)</a>:</h4>
<p>The JSON backend landed btw, it should be in the next nightly :)</p>



<a name="220043033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Get%20a%20crate%27s%20public%20type%20information%3F/near/220043033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Sherret <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Get.20a.20crate&#x27;s.20public.20type.20information.3F.html#220043033">(Dec 15 2020 at 21:12)</a>:</h4>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span> thanks once again for the great tip about using rustdoc's JSON backend! It worked out very well. I was able to code generate this library using the rustdoc output <a href="https://github.com/dsherret/dprint-swc-ecma-ast-view">https://github.com/dsherret/dprint-swc-ecma-ast-view</a> (still working on it to sort out some minor details, but it's basically done)</p>



<a name="220046852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Get%20a%20crate%27s%20public%20type%20information%3F/near/220046852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Get.20a.20crate&#x27;s.20public.20type.20information.3F.html#220046852">(Dec 15 2020 at 21:43)</a>:</h4>
<p>that's awesome! Glad it helped :D</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>